//时间戳转换成日期时间
function js_date_time (unixtime) {
  var date = new Date(unixtime * 1000);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  // return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;//年月日时分秒
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute;

}


/**
 * 输入框只能输入数字的限制
 */
function onInput (num, limit = 2, max, canMinus = false) {
  // num = parseFloat(num) + ''
  let isMinus = false
  let sign = num.substr(0, 1);
  if (canMinus && sign == '-') { 
    isMinus = true
    let endStr = num.substr(num.length-1,1)
    if (num.length == 1) {
      num = ''
    } else { 
      num = -num
      if (endStr == '.') { 
        num = `${num}.`
      }
    }
  }

  let str = num + '';
  let len1 = str.substr(0, 1);
  let len2 = str.substr(1, 1);
  //如果第一位是0，第二位不是点，就用数字把点替换掉
  if (str.length > 1 && len1 == 0 && len2 != ".") {
    str = str.substr(1, 1);
  }
  //第一位不能是.
  if (len1 == ".") {
    str = "";
  }
  //限制只能输入一个小数点
  if (str.indexOf(".") != -1) {
    let str_ = str.substr(str.indexOf(".") + 1);
    if (str_.indexOf(".") != -1) {
      str = str.substr(0, str.indexOf(".") + str_.indexOf(".") + 1);
    }
  }

  if (limit) {
    str = str.replace(/[^\d^\.]+/g, ""); // 保留数字和小数点
    if (limit / 1 === 1) {
      str = str.replace(/^\D*([0-9]\d*\.?\d{0,1})?.*$/, "$1"); // 小数点后只能输 1 位
    } else {
      str = str.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/, "$1"); // 小数点后只能输 2 位
    }
  } else {
    str = str.replace(/[^\d]+/g, ""); // 保留数字和小数点
  }

  if (max && str >= max) {
    return max;
  }

  if (isMinus) {
    return `-${str}`;
  } else { 
    return str;
  }
}

/**
 * @description 绑定事件 on(element, event, handler)
 */
const on = (function () {
  if (document.addEventListener) {
    return function (element, event, handler) {
      if (element && event && handler) {
        element.addEventListener(event, handler, false);
      }
    };
  } else {
    return function (element, event, handler) {
      if (element && event && handler) {
        element.attachEvent("on" + event, handler);
      }
    };
  }
})();

/**
 * 移除某个元素的监听事件
 */
const off = (function () {
  if (document.removeEventListener) {
    return function (element, event, handler) {
      if (element && event) {
        element.removeEventListener(event, handler, false);
      }
    };
  } else {
    return function (element, event, handler) {
      if (element && event) {
        element.detachEvent("on" + event, handler);
      }
    };
  }
})();

/**
 * 10000 => "10,000"
 * @param {number} num
 */
function toThousandFilter (num) {
  return (num || 0)
    .toString()
    .replace(/^-?\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ","));
}

module.exports = {
  js_date_time: js_date_time,
  onInput,
}
